import axios from 'axios';
import qs from 'query-string';

export interface OrderId {
    id: number;
}

export interface OrderRecord {
    id: number;
    order_no: string;
    user_id: number;
    total_amount: number;
    pay_amount: number;
    discount_amount: number;
    status: number;
    pay_status: number;
    pay_time?: number;
    pay_method?: string;
    delivery_status: number;
    delivery_time?: number;
    receiver_name?: string;
    receiver_phone?: string;
    receiver_address?: string;
    remark?: string;
    created_at: number;
    updated_at: number;
    score: number;
    card_score: number;
    card_score_data?: string;
    transaction_id?: string;
    money: number;
    pay_expired_at: number;
    sender_phone?: string;
    refund_status?: number;
    refund_record_id?: number;
    refund_at?: number;
    address_id: number;
    delivery_province?: string;
    delivery_city?: string;
    delivery_district?: string;
    ping_id: number;
    pick_up_point_id: number;
    express_type?: string;
    // 关联数据
    user?: {
        id: number;
        phone: string;
        nickname?: string;
        headimg?: string;
        vip_at: number;
        score: number;
        in_black: number;
    };
    pick_up_point?: {
        id: number;
        name: string;
        address: string;
        phone: string;
    };
    items?: [];
}

export interface OrderItemRecord {
    id: number;
    order_id: number;
    goods_id: number;
    goods_ku_id?: number;
    goods_title: string;
    goods_image?: string;
    sku_specs?: string;
    sku_spec_names?: string;
    unit_price: number;
    quantity: number;
    total_price: number;
    discount_amount: number;
    final_price: number;
    refund_status: number;
    refund_amount: number;
    refund_time?: number;
    refund_reason?: string;
}

export interface OrderListRes {
    list: OrderRecord[];
    total: number;
}

export interface OrderModel {
    id: number;
}

export interface OrderParams extends Partial<OrderRecord> {
    current: number;
    pageSize: number;
    status?: number;
    pay_status?: number;
    express_type?: string;
    keyword?: string;
}

export function queryOrderList(params: OrderParams) {
    return axios.get<OrderListRes>('/admin/order/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取业务详情
 * @param params
 */
export function queryOrderOne(params: OrderId) {
    return axios.get<OrderRecord>('/admin/order/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 删除业务
 * @param id
 */
export function delOrder(id: number) {
    return axios.post('/admin/order/delete', { id });
}

/**
 * 更新订单状态
 * @param id
 * @param status
 */
export function updateOrderStatus(id: number, status: number) {
    return axios.post('/admin/order/updateStatus', { id, status });
}

/**
 * 获取订单状态数据
 */
export function queryOrderStatusData() {
    return axios.get('/admin/order/orderStatusData');
}



